package com.edward.action;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.apache.struts2.ServletActionContext;
import org.springframework.security.annotation.Secured;

import com.edward.JsonDateProcessor;
import com.edward.PagerModel;
import com.edward.model.Cargo;
import com.edward.model.PackingItem;
import com.edward.model.PackingList;
import com.edward.service.CargoManager;
import com.edward.service.PackingItemManager;
import com.edward.service.PackingListManager;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class PackingListAction extends ActionSupport{


	private static final long serialVersionUID = -8174885594511664386L;

	private PackingItemManager packingItemManager;
	
	private PackingListManager packingListManager;
	
	private CargoManager cargoManager;
	
	private PackingList packingList;
	
	private PackingItem packingItem;
	
	private Cargo cargo;
	
	private int limit;
	
	private int start;

	public PackingList getPackingList() {
		return packingList;
	}

	public void setPackingList(PackingList packingList) {
		this.packingList = packingList;
	}

	public PackingItem getPackingItem() {
		return packingItem;
	}

	public void setPackingItem(PackingItem packingItem) {
		this.packingItem = packingItem;
	}
	
	public Cargo getCargo() {
		return cargo;
	}

	public void setCargo(Cargo cargo) {
		this.cargo = cargo;
	}

	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public void setPackingItemManager(PackingItemManager packingItemManager) {
		this.packingItemManager = packingItemManager;
	}

	public void setPackingListManager(PackingListManager packingListManager) {
		this.packingListManager = packingListManager;
	}

	public void setCargoManager(CargoManager cargoManager) {
		this.cargoManager = cargoManager;
	}

	//添加packingList 的表头
	@Secured("ROLE_REPORT_ADD")
	public String add() throws Exception{
		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		packingListManager.addPackingList(packingList);
		int total = packingListManager.listPackingList().size();
		response.getWriter().print("{success : true , msg : '添加成功!',packingList_id:"+ packingList.getId()+",total:" + total + "}");
		return null;
	}
	
	@Secured("ROLE_REPORT_UPDATE")
	public String update() throws Exception{
		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		PackingList b = packingListManager.getPackingList(packingList.getId());
		packingList.setItems(b.getItems());
		packingListManager.updatePackingList(packingList);
		response.getWriter().print("{success : true , msg : '修改成功!'}");
		return null;
	}
	
	@Secured("ROLE_REPORT_UPDATE")
	public String delete() throws Exception{
		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		PackingList s = packingListManager.getPackingList(packingList.getId());
		if(s.getItems().size() != 0){
			response.getWriter().print("{success : false , msg : '删除失败，存在Packing货物列表，请先删除Packing货物!!'}");
		}else{
			packingListManager.deletePackingList(s.getId());
			response.getWriter().print("{success : true , msg : '删除成功!'}");
		}
		return null;
	}

	@Secured("ROLE_REPORT")
	public String getList() throws Exception {

		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
	    JsonConfig cfg = new JsonConfig();
		cfg.registerJsonValueProcessor(java.util.Date.class, new JsonDateProcessor());
		cfg.registerJsonValueProcessor(java.sql.Date.class, new JsonDateProcessor());
		PagerModel pager;
	    pager = packingListManager.pagerList(start, limit );
		JSONObject jsonObject = JSONObject.fromObject(pager,cfg);
		String jsonStr = jsonObject.toString();
		response.getWriter().print(jsonStr);
		return null;
	}
	
	
	
//	//添加packingList 的 货物明细3
	@Secured("ROLE_REPORT_ADD")
	public String addItem() throws Exception{
		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		PackingList p = packingListManager.getPackingList(packingList.getId());
		Cargo c = cargoManager.getCargo(cargo.getId());
		packingItem.setCargo(c);
		packingItem.setSum(packingItem.getPrice()*packingItem.getAmount());
		packingItemManager.addPackingItem(packingItem);
		p.setTotal(p.getTotal() + packingItem.getSum()); 
		p.getItems().add(packingItem);
		packingListManager.updatePackingList(p);
		response.getWriter().print("{success : true , msg : '添加成功!'}");
		return null;
	}
	
	@Secured("ROLE_REPORT_UPDATE")
	public String updateItem() throws Exception {

		ActionContext ctx = ActionContext.getContext();        
		  HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		PackingList p = packingListManager.getPackingList(packingList.getId());
		PackingItem i = packingItemManager.getPackingItem(packingItem.getId());
		packingItem.setSum(packingItem.getPrice()*packingItem.getAmount());
		p.setTotal(p.getTotal() + packingItem.getSum() - i.getSum()); 
		packingItem.setCargo(i.getCargo());
		packingListManager.updatePackingList(p);
		packingItemManager.updatePackingItem(packingItem);
		response.getWriter().print("{success : true , msg : '修改成功!'}");
		return null;
	}
	
	@Secured("ROLE_REPORT_UPDATE")
	public String deleteItem() throws Exception {

		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		PackingItem c = packingItemManager.getPackingItem(packingItem.getId());
		PackingList s = packingListManager.getPackingList(packingList.getId());
		s.setTotal(s.getTotal() - c.getSum() ); 
		packingListManager.updatePackingList(s);
		packingItemManager.deletePackingItem(c.getId());
		response.getWriter().print("{success : true , msg : '删除成功'}");
		return null;
	}


	//获取grid 显示 的 分页信息
	@Secured("ROLE_REPORT")
	public String getItems() throws Exception {

		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		JsonConfig cfg = new JsonConfig();
		cfg.registerJsonValueProcessor(java.util.Date.class, new JsonDateProcessor());
		cfg.registerJsonValueProcessor(java.sql.Date.class, new JsonDateProcessor());
		PagerModel pager;
	    pager = packingItemManager.pagerPackingItem(start,limit,packingList.getId());
		JSONObject jsonObject = JSONObject.fromObject(pager,cfg);
		String jsonStr = jsonObject.toString();
		response.getWriter().print(jsonStr);
		return null;
	}
	
	@Secured("ROLE_REPORT")
	 public String getListJudge() throws Exception {
			ActionContext ctx = ActionContext.getContext();        
			HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
			response.setContentType("text/html;charset=UTF-8");
				response.getWriter().print("{success : true , msg : '成功!',deny:false}");
		 return null;
	 }
	
	
	
	
	
	
	
	
	

}
